package com.study.animationstudy.ui

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import com.study.animationstudy.R
import com.study.animationstudy.base.BaseActivity
import com.study.animationstudy.databinding.ActivityObjectGroupBinding

class ObjectGroupActivity : BaseActivity<ActivityObjectGroupBinding>(),OnClickListener,AnimatorListener {

    // 声明一个属性动画组合对象
    private lateinit var animSet:AnimatorSet
    private var isPaused = false

    override fun ActivityObjectGroupBinding.initBinding() {
        mBinding.ivObjectGroup.setOnClickListener(this@ObjectGroupActivity)
        initObjectAnim()
    }

    private fun initObjectAnim(){
        // 构造一个在横轴上平移的属性动画
        val anim1 = ObjectAnimator.ofFloat(mBinding.ivObjectGroup,"translationX",0f,100f)
        // 构造一个在透明度上变化的属性动画
        val anim2 = ObjectAnimator.ofFloat(mBinding.ivObjectGroup,"alpha",1f,0.1f,1f,0.5f,1f)
        // 构造一个围绕中心点旋转的属性动画
        val anim3 = ObjectAnimator.ofFloat(mBinding.ivObjectGroup,"rotation",0f,360f)
        // 构造一个在纵轴上缩放的属性动画
        val anim4 = ObjectAnimator.ofFloat(mBinding.ivObjectGroup,"scaleY",1f,0.5f,1f)
        // 构造一个在横轴上平移的属性动画
        val anim5 = ObjectAnimator.ofFloat(mBinding.ivObjectGroup,"translationX",100f,0f)
        // 创建一个属性动画组合
        animSet = AnimatorSet()
        // 把指定的属性动画添加到属性动画组合
        val builder = animSet.play(anim2)
        // 动画播放顺序为：先执行anim1，再一起执行anim2、anim3、anim4，最后执行anim5
        builder.with(anim3).with(anim4).after(anim1).before(anim5)
        // 设置动画的播放时长
        animSet.duration = 4500
        // 开始播放属性动画
        animSet.start()
        // 给属性动画添加动画事件监听器
        animSet.addListener(this)

    }

    override fun onClick(v: View?) {
        when(v?.id){
            mBinding.ivObjectGroup.id -> {
                if (animSet.isStarted){
                    if (!isPaused){
                        Log.d("Tag","暂停播放")
                        animSet.pause()
                    } else {
                        Log.d("Tag","继续播放")
                        animSet.resume()
                    }
                    // 恢复播放属性动画
                    isPaused = !isPaused
                } else {// 属性动画尚未开始播放
                    Log.d("Tag","开始播放")
                    animSet.start()
                }
            }
        }
    }

    override fun onAnimationStart(animation: Animator?) {

    }

    override fun onAnimationEnd(animation: Animator?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            // 设置当前播放的时间点
            animSet.currentPlayTime = 0
            // 从动画尾巴开始倒播至setCurrentPlayTime设置的时间点
            animSet.reverse()
        }
    }

    override fun onAnimationCancel(animation: Animator?) {

    }

    override fun onAnimationRepeat(animation: Animator?) {

    }

}